<template>
  <div class="layout-container">
    <el-container class="layout-container-main">
      <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
        <Aside :is-collapse="isCollapse" />
      </el-aside>
      
      <el-container class="main-container">
        <el-header class="header">
          <div class="header-left">
            <el-icon class="fold-icon" @click="toggleCollapse">
              <Fold v-if="!isCollapse" />
              <Expand v-else />
            </el-icon>
            <div class="breadcrumb">
              后台管理系统
            </div>
          </div>
          <div class="header-right">
            <el-dropdown class="user-dropdown">
              <div class="user-info">
                <el-avatar :size="32" class="user-avatar">{{ userName?.charAt(0)?.toUpperCase() }}</el-avatar>
                <span class="user-name">欢迎您，{{ userName }}</span>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="personal">
                    <el-icon><User /></el-icon>
                    个人信息
                  </el-dropdown-item>
                  <el-dropdown-item @click="password">
                    <el-icon><Lock /></el-icon>
                    修改密码
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="logout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <el-main class="main">
          <div class="content-container">
            <router-view />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import Aside from "@/components/Aside.vue";
import { ref } from 'vue';
import request from '@/utils/request'
import { useRouter } from "vue-router";
import { User, Lock, SwitchButton, Fold, Expand } from '@element-plus/icons-vue'

const router = useRouter()
const userName = localStorage.getItem("userName")
const isCollapse = ref(false)

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const logout = async() => {
  try {
    await request.get('/user/logout')
    localStorage.removeItem('token')
    localStorage.removeItem('userName')
    localStorage.removeItem('menus')
    localStorage.removeItem('userType')
    router.push('/login')
  } catch (error) {
    console.error('登出失败:', error)
  }
}

const personal = () => {
  router.push('/personal')
}

const password = () => {
  router.push('/password')
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  background-color: #f5f7f9;
}

.layout-container-main {
  height: 100%;
}

.main-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: all 0.3s;
}

.aside {
  background-color: #1d1e1f;
  transition: width 0.3s;
  overflow-x: hidden;
  box-shadow: 2px 0 6px rgba(0,21,41,.1);
  z-index: 999;
}

.header {
  background: white;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  position: relative;
  z-index: 99;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.fold-icon {
  font-size: 20px;
  cursor: pointer;
  color: #606266;
  padding: 8px;
  border-radius: 4px;
  transition: all 0.3s;
}

.fold-icon:hover {
  background-color: #f6f6f6;
  color: #409EFF;
}

.breadcrumb {
  font-size: 18px;
  font-weight: 500;
  color: #303133;
  margin-left: 8px;
}

.header-right {
  display: flex;
  align-items: center;
  margin-right: 50px;
}

.user-dropdown {
  cursor: pointer;
  margin-left: auto;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 5px 12px;
  border-radius: 4px;
  transition: all 0.3s;
  background: transparent;
}

.user-info:hover {
  background: #f6f6f6;
}

.user-avatar {
  background: linear-gradient(135deg, #409EFF, #3a8ee6);
  color: white;
  font-weight: bold;
}

.user-name {
  margin: 0 8px;
  font-size: 14px;
  color: #606266;
}

.el-icon--right {
  font-size: 12px;
  margin-left: 4px;
  color: #909399;
}

.main {
  padding: 20px;
  background: #f5f7f9;
  min-height: calc(100vh - 60px);
  box-sizing: border-box;
  position: relative;
}

.content-container {
  background: white;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  padding: 20px;
  min-height: calc(100% - 40px);
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  padding: 8px 20px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 10px;
  font-size: 16px;
}

@media screen and (max-width: 768px) {
  .aside {
    position: fixed;
    height: 100%;
  }
  
  .header {
    padding: 0 15px;
  }
  
  .user-name {
    display: none;
  }
  
  .main {
    padding: 15px;
  }
  
  .content-container {
    padding: 15px;
  }
}
</style>
